<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-left">
        <div class="logo">LOGO</div>
      </div>
      <div class="register-right">
        <h2>注册账号</h2>
        <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
          <el-form-item label="Username" prop="username">
            <el-input v-model="form.username" autocomplete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="Password" prop="password">
            <el-input type="password" v-model="form.password" autocomplete="off" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item label="Confirm Password" prop="confirmPassword">
            <el-input type="password" v-model="form.confirmPassword" autocomplete="off" placeholder="请确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">Register</el-button>
            <router-link to="/login" class="login-link">已有账号？去登录</router-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { register } from '../api/loginApi';

const formRef = ref(null);
const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
});
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.value.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur',
    },
  ],
};

const onSubmit = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const data = await register({ username: form.value.username, password: form.value.password });
      if (data.code === 200) {
        alert('注册成功!');
      } else {
        alert(data.message);
      }
    } else {
      console.log('参数校验失败!');
      return false;
    }
  });
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  position: relative;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(232, 224, 224, 0.5);
  z-index: 1;
}

.register-content {
  display: flex;
  max-width: 1000px;
  width: 100%;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  overflow: hidden;
  z-index: 2;
}

.register-left {
  flex: 1;
  background-image: url('/path/to/your/image.jpg');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 300px;
}

.logo {
  font-size: 2em;
  color: white;
}

.register-right {
  width: 400px;
  padding: 40px;
  background-color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

h2 {
  color: var(--primary-color, #409eff);
  margin-bottom: 20px;
  text-align: center;
}

.login-link {
  margin-left: 20px;
  color: var(--primary-color, #409eff);
}
</style>